package com.jbm.jbmsupplier.adapter;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.activity.address.audiovideo.VideoCamer;
import com.jbm.jbmsupplier.activity.address.presenter.MyAddressReClass;
import com.jbm.jbmsupplier.zp.liaotian.ShowImageView;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by 家帮忙 on 2016/9/28.
 */

public class MyRecy_photoAdapter extends RecyclerView.Adapter<MyRecy_photoAdapter.MyViewHolder> {
    Context c;
    ArrayList<HashMap<String ,String>> listrecy;
    MyAddressReClass.OnclicAdapter onclicAdapter;
    private MediaPlayer mMediaPlayer;

    public MyRecy_photoAdapter(Context c, ArrayList<HashMap<String ,String>> listrecy, MyAddressReClass.OnclicAdapter onclicAdapter) {
        this.c = c;
        this.listrecy = listrecy;
        this.onclicAdapter=onclicAdapter;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(View.inflate(c, R.layout.item_recyl_photo_image,null));
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
     if (listrecy.get(position).get("audio").equals("pic")){
         holder.img.setTag(position);
         Picasso.with(c).load(new File(listrecy.get(Integer.parseInt(holder.img.getTag()+"")).get("pic"))).resize(135,135).into(holder.img);
     }else if (listrecy.get(position).get("audio").equals("video")){
         holder.videobofang.setVisibility(View.VISIBLE);
         holder.img.setTag(position);
         Picasso.with(c).load(new File(listrecy.get(Integer.parseInt(holder.img.getTag()+"")).get("pic"))).resize(135,135).into(holder.img);
     }else if (listrecy.get(position).get("audio").equals("audio")){
         holder.img.setTag(position);
         Picasso.with(c).load(R.mipmap.yuyinbofang).resize(135,135).into(holder.img);
     }
        onclicAdapter.getDaTaList(listrecy);
if (listrecy.size()>0) {
    holder.delete.setVisibility(View.VISIBLE);
    holder.delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listrecy.remove(listrecy.get((Integer.parseInt(holder.img.getTag()+""))));
            notifyDataSetChanged();
            onclicAdapter.getDaTaList(listrecy);

        }
    });

}
        holder.videobofang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               c.startActivity(new Intent(c, VideoCamer.class).putExtra("url",listrecy.get(Integer.parseInt(holder.img.getTag()+"")).get("path")));
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listrecy.get(position).get("audio").equals("pic")) {
                    c.startActivity(new Intent(c, ShowImageView.class).putExtra("image", listrecy.get(Integer.parseInt(holder.img.getTag() + "")).get("pic")));
                }else if (listrecy.get(position).get("audio").equals("audio")){
                    playMusic(listrecy.get(Integer.parseInt(holder.img.getTag() + "")).get("path"));

                }
            }
        });




    }


    private void playMusic(String name) {
        try {
            mMediaPlayer = new MediaPlayer();
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.stop();
            }
            mMediaPlayer.reset();
            mMediaPlayer.setDataSource(name);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
            mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                public void onCompletion(MediaPlayer mp) {


                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
public MediaPlayer getMediaplay(){
    return mMediaPlayer;
}
    @Override
    public int getItemCount() {
        return listrecy.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

ImageView delete,img,videobofang;
        public MyViewHolder(View itemView) {
            super(itemView);
            videobofang= (ImageView) itemView.findViewById(R.id.videobofang);
            delete= (ImageView)itemView.findViewById(R.id.delete);
            img= (ImageView)itemView.findViewById(R.id.selectImg);
        }
    }

    public void setNotifi(ArrayList<HashMap<String ,String>> listrecy){
        this.listrecy=listrecy;
        notifyDataSetChanged();

    }

}
